#include "test21_StlConOfVector.h"
#include <iostream>


void test21_StlConOfVector::classMain()
{
	std::cout << "test21_StlConOfVector" << "\n";
	this->testVector();
}


// 动态数组，向量
void test21_StlConOfVector::testVector()
{
	std::vector<int> myVect; //采用模板实现类实现，默认构造函数
	myVect.push_back(1);
	const int NUM = 5;
	// vector，动态数组，向量
	std::vector <std::string>names(NUM);    //定义矢量对象
	std::vector <int> sexs(NUM);    //同上
	std::cout << "Please Do Exactly As Told You Will enter \n" << NUM << " Personal Name and Their Sex.\n";
	int i = 0;
	for (i = 0; i < NUM; i++)    //输入信息
	{
		std::cout << "Enter title # " << i + 1 << ": ";
		std::getline(std::cin, names[i]); //获取输入信息
		std::cout << "Enter sex (0/1) #";
		std::cin >> sexs[i];    //获取输入信息
		std::cin.get();    //等待
		// std::cin.get()=从控制台获取单个输入字符，包括空格、制表符和换行符
		// std::getline(input_stream, str_variable, delimiter) = 用于从输入流中读取一行字符串
		//		input_stream 是要读取的输入流对象，比如 std::cin 用于读取标准输入，或者一个文件流对象。
		//		str_variable 是接收读取到的字符串的变量，它可以是 std::string 类型的对象，也可以是字符数组（C - style string）。
		//		delimiter 是可选参数，表示行结束的分隔符，默认为换行符 \n。你也可以指定其他字符作为分隔符，比如逗号, 或制表符 \t。
	}
	std::cout << "Thank you. You entered the following:    \n" << "name/sex" << std::endl;
	for (i = 0; i < NUM; i++)    //输出信息
	{
		std::cout << names[i] << "\t" << sexs[i] << std::endl;
	}
}


// 向量测试
void test21_StlConOfVector::testVector(long& value)
{
	std::cout << "动态数组，向量";
	std::vector<std::string> c;
	char buf[10];
	clock_t timeStart = clock(); // 计时函数

	for (long i = 0; i < value; ++i)
	{
		try {
			snprintf(buf, 10, "%d", rand()); // 将格式化的数据写入字符串缓冲区，可以指定格式化字符串的值
			c.push_back(std::string(buf)); // 插入数据
		}
		catch (std::exception& p) {
			std::cout << i << p.what() << std::endl;
			abort();
		}
	}
	// 常用的方法
	std::cout << (clock() - timeStart) << std::endl;
	//std::cout << c.begin() << std::endl; // 返回指向第一个元素的迭代器
	std::cout << c.size() << std::endl;
	std::cout << c.front() << std::endl;
	std::cout << c.back() << std::endl;
	std::cout << c.data() << std::endl;
	std::cout << c.capacity() << std::endl;
}


// 
void test21_StlConOfVector::testArray()
{
#define ASIZE 10; // 宏定义
	std::cout << "";
	std::array<long, 10> c;
	clock_t timeStart = clock();
	for (long i = 0; i < 10; ++i) {
		c[i] = rand();
	}

	//
	std::cout << (clock() - timeStart) << std::endl;
	std::cout << c.size() << std::endl;
	std::cout << c.front() << std::endl;
	std::cout << c.back() << std::endl;
	std::cout << c.data() << std::endl;

	long target;
	timeStart = clock();
	//qsort(c.data(), 10, sizeof(long), compareLongs);
	//long* pItem = (long*)bsearch(&target, (c.data(), 10, sizeof(long));
	//cout << (clock() - timeStart) << endl;
	//if (pItem != NULL)
	//	cout << *pItem << endl;
	//else
	//	cout << "not found" << endl;
}

